// 导入jwt
const jwt = require("jsonwebtoken");

// 创建(生成)token
// let token = jwt.sign(用户数据,加密字符串,配置对象);
let token = jwt.sign(
  {
    username: "张三",
  },
  "fhjksadh", // 加密字符串
  {
    expiresIn: 60, // 设置生命周期,单位为秒
  }
);
console.log(token);
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IuW8oOS4iSIsImlhdCI6MTcwNjM1NjcwOCwiZXhwIjoxNzA2MzU2NzY4fQ.x5BDKVKmPPrSD6WyZRomDbNOudfn3FHMOu3304L7ZgI
let t = token;

// 校验token
jwt.verify(t, "fhjksadh", (err, data) => {
  if (err) {
    console.log("校验失败");
    return;
  } else {
    console.log(data);  //{ username: '张三', iat: 1706356880, exp: 1706356940 }
  }
});
